/**
 * MatchGameView
 * 
 * @author Joshua Stein
 *
 */
package edu.bu.cs673.superkids123.children.view.games;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractNavigationView;
import edu.bu.cs673.superkids123.children.controller.games.MatchGameController;
import edu.bu.cs673.superkids123.children.model.resources.AudioPlayer;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResource;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
import edu.bu.cs673.superkids123.children.view.components.GraphicsImage;
import edu.bu.cs673.superkids123.children.view.components.ViewManager;
public class MatchGameView extends AbstractNavigationView {
	
	public static final int NUMBER_OF_ANSWER_CHOICES = 5;
	private boolean readyToPaint = false;
	private JPanel matchGamePanel;
	private MatchGameController controller;
	private ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();
	private ImageIcon answerCenter, next, option, background;
	private ViewManager position;
	private HashMap<String, GraphicsImage> design;
	private boolean tryagain;
	
	public MatchGameView(AbstractFrame mainFrame) {
		super(mainFrame);
		controller = getMainFrame().getController(MatchGameController.class);
		setDesignAndImages();
		position = new ViewManager(design);
	}
	@Override
	public String getTitle() {
		return "Match Game";
	}
	@Override
	public String getBorderTitle() {
		return "Match Game";
	}
	@Override
	protected JComponent layout() {
		
		ImageIcon backgroundImage = provider.getImage(ChildrenResource.background);
		
		JLabel lab = new JLabel("",backgroundImage, JLabel.CENTER);
		
		matchGamePanel = new JPanel(new BorderLayout()) {
		
			private static final long serialVersionUID = 1L;
			public void paint(Graphics g) {
				super.paint(g);
				if (readyToPaint) {	
					position.draw("background", g, background.getImage());
					if (tryagain) {
						g.drawString("Try again", 20, 20);
						
					}
					answerCenter = controller.currentQuestion.getAnswerImage();
					position.draw("A", g, answerCenter.getImage());
					for( int i=1; i<=5; i++){
						option = controller.currentQuestion.atAnswerOption(i);
						position.draw(String.valueOf(i), g, option.getImage());
					}	
					position.draw("->", g, next.getImage());
				}
			}
		};
		
		HandlerClass handler = new HandlerClass(this);
		matchGamePanel.addMouseListener(handler);
		matchGamePanel.add(lab, BorderLayout.CENTER);
		readyToPaint = true;
		return matchGamePanel;
	}
	public void tryAgainOff() {
		tryagain=false;
		reset();		
	}
	public void tryAgainOn() {
		tryagain=true;
		reset();
	}
	private void reset(){
		matchGamePanel.revalidate();
		matchGamePanel.repaint();
	}
	private class HandlerClass implements MouseListener{
		MatchGameView view;
		public HandlerClass(MatchGameView view){
			this.view = view;
		}
// ALL CLICK REACTIONS
		public void mouseClicked(MouseEvent p) {
			//System.out.println( p.getX()+ " " +  p.getY());  uncomment when looking for position values during design
			String clicked = position.whatsAt( p.getX(),  p.getY() );
			if(  clicked.equals(  "->"  ) ){
				controller.nextQuestion();
				tryAgainOff();
				view.matchGamePanel.revalidate();
				view.matchGamePanel.repaint();
			}
			if(  clicked.equals(  "1"  )  |  clicked.equals(  "2"  ) | clicked.equals(  "3"  ) | clicked.equals( "4" ) | clicked.equals( "5" ) )	{	
				int optionNumber = Integer.parseInt(clicked);
				playAudioForNumber(controller.currentQuestion.getOption( optionNumber ) );	
				controller.answerButtonPressed( view, clicked );
			}
			if(  clicked.equals(  "A"  )  )
				playAudioForNumber(controller.currentQuestion.getAnswer());
		}
// END
		public void mouseEntered(MouseEvent arg0) {}public void mouseExited(MouseEvent arg0) {}
		public void mousePressed(MouseEvent arg0) {}public void mouseReleased(MouseEvent arg0) {}
	}
	public void playAudio(byte[] audioData){
		AudioPlayer audioPlayer = new AudioPlayer();
		audioPlayer.playAudio(audioData);
		try {
			Thread.sleep(100);
		}
		catch (InterruptedException ie) {
			System.out.println("Thread interrupted");
		}
	}
	
	public void playAudioForNumber(int number) {
		AudioPlayer audioPlayer = new AudioPlayer();
		byte[] audioData = getAudioSampledData(number); 
		audioPlayer.playAudio(audioData);
		try {
			Thread.sleep(100);
		}
		catch (InterruptedException ie) {
			System.out.println("Thread interrupted");
		}
	}
	
	public byte[] getAudioSampledData(int numberToPlay) {
		byte[] audioData = null;
		audioData = provider.getAudioContentByNumber(numberToPlay);
		return audioData;
	}
	
	private void setDesignAndImages(){
		background = provider.getImage(ChildrenResource.clickGameBackground);
		next = provider.getImage(ChildrenResource.next);//provider is used here, I don't like that it exists in different code areas at random.
		design = new HashMap<String, GraphicsImage>();
// ALL DESIGN HERE
					/*put("name", new GraphicsImage("Name", pos_x, pos_y, width, height)); -FORMAT*/ 
					design.put("background", new GraphicsImage("background", 0,0,360,750));
					design.put("A", new GraphicsImage("A", 200, -30, 180, 180));
					design.put("1", new GraphicsImage("1", -5, 30, 100, 100));
					design.put("2", new GraphicsImage("2", 5, 245, 100, 100));
					design.put("3", new GraphicsImage("3", 195, 260, 100, 100));
					design.put("4", new GraphicsImage("4", 643, 252, 100, 100));
					design.put("5", new GraphicsImage("5", 650, 130, 100, 100));
					design.put("->", new GraphicsImage("->", 650, 0, 50, 50));
// END				
	}
}